﻿using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CObject;
using Objects;
using Objects.CMT;

public partial class ParticipantRegistration : cPage
{

    int eventId = 0;
    int eventAttendeeId = 0;

    #region Page_Load
    /// <summary>
    /// Page load event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["EventID"] != null)
            {
                eventId = Convert.ToInt32(Request.QueryString["EventID"]);

                if (Request.QueryString["EventAttendeeID"] != null)
                {
                    eventAttendeeId = Convert.ToInt32(Request.QueryString["EventAttendeeID"]);
                    Banner1.Visible = false;
                    tblRegisteredParticipants.Visible = false;
                }

                lblMessage.Text = string.Empty;
                tblParticipantRegistration.Visible = true;

                if (!Page.IsPostBack)
                {
                    bool isThereAnyPrt = false;

                    // Method to bind the event participant drop down.
                    DataTable dtEventAttendee = BindEventParticipant(eventId, ref isThereAnyPrt);

                    if (isThereAnyPrt)
                    {
                        bool isValid = true;

                        if (eventAttendeeId != 0)
                        {
                            ddlParticipant.SelectedValue = eventAttendeeId.ToString();
                            ddlParticipant.Enabled = false;
                            lnkExportToExcell.Visible = false;
                        }

                        // Method to bind event questions.
                        BindEventQuestions();

                        // Method to bind the no of check box with event start date till end date.
                        BindNoOfNightsCheckBox(ref isValid);

                        if (isValid)
                        {
                            // Method to bind registered participants.
                            BindRegisteredParticipants();

                            // Method to bind the current event participant's registration details.
                            BindEventParticipantDetails(dtEventAttendee);
                        }
                    }
                    else
                    {
                        tblParticipantRegistration.Visible = false;
                        lblMessage.Text = ParticipantRegistrationClass.NoParticipantsAdded;
                        lblMessage.ForeColor = Color.Red;
                    }
                }
            }
            else
            {
                tblParticipantRegistration.Visible = false;
                lblMessage.Text = ParticipantRegistrationClass.RegistrationPageAccessDenied;
                lblMessage.ForeColor = Color.Red;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region BindRegisteredParticipants
    /// <summary>
    /// Method to bind registered participants.
    /// </summary>
    private void BindRegisteredParticipants()
    {
        ParticipantRegistrationClass getRegisteredParticipants = new ParticipantRegistrationClass();
        lstRegisteredParticipants.DataSource = getRegisteredParticipants.GetRegisteredParticipants(eventId);
        lstRegisteredParticipants.DataBind();
    }
    #endregion

    #region BindNoOfNightsCheckBox
    /// <summary>
    /// Method to bind the no of check box with event start date till end date.
    /// </summary>
    private void BindNoOfNightsCheckBox(ref bool isValid)
    {
        CMTEvent eventsColl = new CMTEvent();
        DataTable dtEventList = eventsColl.GetSpecificEvent(eventId);

        if (dtEventList.Rows.Count > 0)
        {
            DataRow drEventDetails = dtEventList.Rows[0];           
            if (drEventDetails["IsQuestionConfigured"].ToString() == "1")
            {
                DateTime dtEventStartDate = Convert.ToDateTime(drEventDetails["StartDate"].ToString());
                DateTime dtEventEndDate = Convert.ToDateTime(drEventDetails["EndDate"].ToString());
                lblEventName.Text = drEventDetails["EventName"].ToString();

                for (DateTime dt = dtEventStartDate; dtEventStartDate <= dtEventEndDate; dtEventStartDate = dtEventStartDate.AddDays(1))
                {
                    cblNoOfNights.Items.Add(dtEventStartDate.ToShortDateString());
                }
            }
            else
            {
                isValid = false;
                tblParticipantRegistration.Visible = false;
                lblMessage.Text = ParticipantRegistrationClass.NoQuestiondConfigured;
                lblMessage.ForeColor = Color.Red;
            }            
        }
        else
        {
            isValid = false;
            tblParticipantRegistration.Visible = false;
            lblMessage.Text = ParticipantRegistrationClass.RegistrationPageAccessDenied;
            lblMessage.ForeColor = Color.Red;
        }
    }
    #endregion

    #region BindEventParticipantDetails
    /// <summary>
    /// Method to bind the current event participant's registration details.
    /// </summary>
    /// <param name="dtEventAttendee"></param>
    private void BindEventParticipantDetails(DataTable dtEventAttendee)
    {
        if (eventAttendeeId != 0)
        {
            DataRow drCurrentEventAttendee = (dtEventAttendee.Select("EventAttendeeID = '" + eventAttendeeId.ToString() + "'"))[0];
            lblFName.Text = drCurrentEventAttendee["FirstName"].ToString();
            lblLName.Text = drCurrentEventAttendee["LastName"].ToString();
            lblEid.Text = drCurrentEventAttendee["EmailAddress"].ToString();

            ParticipantRegistrationClass prtRegistration = new ParticipantRegistrationClass();
            prtRegistration.EventAttendeeId = eventAttendeeId;
            CCollection loColl = new CCollection(prtRegistration, true);
            DataTable dtParticipantRegistrationDetails = loColl.DataTable;

            foreach (DataRow drParticipantRegistration in dtParticipantRegistrationDetails.Rows)
            {
                string questionCode = drParticipantRegistration["Code"].ToString();
                string answer = drParticipantRegistration["Answer"].ToString();

                switch (questionCode)
                {
                    case "Q1":
                        ddlSalutation.SelectedIndex = -1;

                        if (ddlSalutation.Items.FindByText(answer.Trim()) != null)
                            ddlSalutation.Items.FindByText(answer.Trim()).Selected = true;
                        break;
                    case "Q2":
                        txtPrefAdd.Text = answer;
                        break;
                    case "Q3":
                        txtOffice.Text = answer;
                        break;
                    case "Q4":
                        txtCountryRep.Text = answer;
                        break;
                    case "Q5":
                        if (rblConfAtt.Items.FindByText(answer) != null)
                            rblConfAtt.Items.FindByText(answer).Selected = true;
                        break;
                    case "Q6":
                        txtArrDate.Text = answer;
                        break;
                    case "Q7":
                        string[] arrtime = answer.Split(':');
                        ddlArrTimeHrs.SelectedIndex = -1;
                        ddlArrTimeMns.SelectedIndex = -1;

                        if (arrtime.Length == 2)
                        {
                            if (ddlArrTimeHrs.Items.FindByText(arrtime[0]) != null)
                                ddlArrTimeHrs.Items.FindByText(arrtime[0]).Selected = true;

                            if (ddlArrTimeMns.Items.FindByText(arrtime[1]) != null)
                                ddlArrTimeMns.Items.FindByText(arrtime[1]).Selected = true;
                        }

                        break;
                    case "Q8":
                        txtArrAirport.Text = answer;
                        break;
                    case "Q9":
                        txtArrAirline.Text = answer;
                        break;
                    case "Q10":
                        txtDeptDate.Text = answer;
                        break;
                    case "Q11":
                        string[] deptTime = answer.Split(':');
                        ddlDeptTimeHrs.SelectedIndex = -1;
                        ddlDeptTimeMns.SelectedIndex = -1;

                        if (deptTime.Length == 2)
                        {
                            if (ddlDeptTimeHrs.Items.FindByText(deptTime[0]) != null)
                                ddlDeptTimeHrs.Items.FindByText(deptTime[0]).Selected = true;

                            if (ddlDeptTimeMns.Items.FindByText(deptTime[1]) != null)
                                ddlDeptTimeMns.Items.FindByText(deptTime[1]).Selected = true;
                        }

                        break;
                    case "Q12":
                        txtDeptAirport.Text = answer;
                        break;
                    case "Q13":
                        txtDeptAirline.Text = answer;
                        break;
                    case "Q14":
                        txtHotelName.Text = answer;
                        break;
                    case "Q15":
                        txtHotelLocation.Text = answer;
                        break;
                    case "Q16":
                        txtDualHotel.Text = answer;
                        break;
                    case "Q17":
                        string[] noOfNights = answer.Split(',');

                        foreach (string noOfNight in noOfNights)
                        {
                            if (cblNoOfNights.Items.FindByText(noOfNight) != null)
                                cblNoOfNights.Items.FindByText(noOfNight).Selected = true;
                        }

                        break;
                    case "Q18":
                        string[] otherPrefs = answer.Split(',');

                        foreach (string otherPref in otherPrefs)
                        {
                            if (cblOtherPref.Items.FindByText(otherPref) != null)
                                cblOtherPref.Items.FindByText(otherPref).Selected = true;
                        }

                        break;
                    case "Q19":
                        if (rblNetEvent.Items.FindByText(answer) != null)
                            rblNetEvent.Items.FindByText(answer).Selected = true;
                        break;
                    case "Q20":
                        string[] dietReqs = answer.Split(',');

                        foreach (string dietReq in dietReqs)
                        {
                            if (cblDietReq.Items.FindByText(dietReq) != null)
                                cblDietReq.Items.FindByText(dietReq).Selected = true;
                        }

                        break;
                    case "Q21":
                        if (rblAvailToPlay.Items.FindByText(answer) != null)
                            rblAvailToPlay.Items.FindByText(answer).Selected = true;
                        break;
                    case "Q22":
                        txtDirectDial.Text = answer;
                        break;
                    case "Q23":
                        txtMobNo.Text = answer;
                        break;
                    case "Q24":
                        txtAssistantName.Text = answer;
                        break;
                    case "Q25":
                        txtAssistantTelno.Text = answer;
                        break;
                    case "Q26":
                        txtAssistantEmailID.Text = answer;
                        break;
                    case "Q27":
                        if (rblVisaReq.Items.FindByText(answer) != null)
                            rblVisaReq.Items.FindByText(answer).Selected = true;
                        break;
                    case "Q28":
                        txtSpecialReq.Text = answer;
                        break;
                    case "Q29":
                        txtCPEAcc.Text = answer;
                        break;
                    case "Q30":
                        if (rblCBSubmitted.Items.FindByText(answer) != null)
                            rblCBSubmitted.Items.FindByText(answer).Selected = true;
                        break;
                    case "Q31":
                        txtOtherComments.Text = answer;
                        break;
                }
            }

            lblFirstName.Visible = true;
            lblLastName.Visible = true;
            lblEmailID.Visible = true;
        }
        else
        {
            lblFirstName.Visible = false;
            lblLastName.Visible = false;
            lblEmailID.Visible = false;
        }
    }
    #endregion

    #region BindEventQuestions
    /// <summary>
    ///  Method to bind event questions.
    /// </summary>
    private void BindEventQuestions()
    {
        Questions eventQuestions = new Questions();
        eventQuestions.EventId = eventId;
        CCollection loColl = new CCollection(eventQuestions, true);
        DataTable dtEventQuestions = loColl.DataTable;

        foreach (DataRow drEventQuestion in dtEventQuestions.Rows)
        {
            bool isMappedWithEvent = Convert.ToBoolean(drEventQuestion["IsMapped"].ToString());

            if (isMappedWithEvent)
            {
                string questionCode = drEventQuestion["Code"].ToString();

                if (tblParticipantRegistration.FindControl(questionCode) != null)
                {
                    HtmlTableRow trCurrent = (HtmlTableRow)tblParticipantRegistration.FindControl(questionCode);
                    trCurrent.Style.Value = "padding-top: 5px; padding-left: 2px; display: block;";
                }
            }
        }
    }
    #endregion

    #endregion

    #region BindEventParticipant
    /// <summary>
    /// Method to bind the event participant drop down.
    /// </summary>
    /// <param name="eventId"></param>
    private DataTable BindEventParticipant(int eventId, ref bool isThereAnyPrt)
    {
        // Binding Event Participant listbox.
        EventAttendee eventAttendee = new EventAttendee();
        eventAttendee.EventID = eventId;
        CCollection loColl = new CCollection(eventAttendee, true);
        DataTable dtEventAttendee = loColl.DataTable;
        Session["EventAttendee"] = dtEventAttendee;
        DataRow[] drEventParticipants = dtEventAttendee.Select("IsClient = 'false'");

        if (drEventParticipants.Count() > 0)
        {
            isThereAnyPrt = true;
            ddlParticipant.DataSource = drEventParticipants.CopyToDataTable();
            ddlParticipant.DataBind();
            ListItem defaultItem = new ListItem("Select Participant", "0");
            ddlParticipant.Items.Insert(0, defaultItem);
            ddlParticipant.SelectedIndex = 0;
        }

        return dtEventAttendee;
    }
    #endregion

    #region ddlParticipant_SelectedIndexChanged
    /// <summary>
    /// Event to get the entered registration details from DB.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlParticipant_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            // Method to clear all the entered fields.
            lblFirstName.Visible = false;
            lblLastName.Visible = false;
            lblEmailID.Visible = false;
            lblFName.Text = string.Empty;
            lblLName.Text = string.Empty;
            lblEid.Text = string.Empty;

            ClearAll();

            if (ddlParticipant.SelectedIndex != -1 && ddlParticipant.SelectedIndex != 0)
            {
                DataTable dtEventAttendee = new DataTable();

                if (Session["EventAttendee"] != null)
                {
                    dtEventAttendee = (DataTable)Session["EventAttendee"];
                    eventAttendeeId = Convert.ToInt32(ddlParticipant.SelectedValue);

                    // Method to bind the current event participant's registration details.
                    BindEventParticipantDetails(dtEventAttendee);
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region lnkExportToExcell_Click
    /// <summary>
    /// Event to export all the registration details to excell.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkExportToExcell_Click(object sender, EventArgs e)
    {
        try
        {
            ParticipantRegistrationClass getRegisteredParticipants = new ParticipantRegistrationClass();
            DataTable dtToExport = getRegisteredParticipants.GetRegisteredParticipantsToExportToXL(eventId);

            string fileName = "attachment;filename= ParticipantRegistrationDetails_" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xls";
            Response.Clear();
            Response.AddHeader("content-disposition", fileName);
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/excel";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            GridView gv = new GridView();
            gv.DataSource = dtToExport;
            gv.HeaderStyle.BackColor = Color.FromName("#11459E");
            gv.HeaderStyle.Font.Bold = true;
            gv.HeaderStyle.Font.Name = "Arial";
            gv.HeaderStyle.Font.Size = FontUnit.XSmall;
            gv.HeaderStyle.ForeColor = Color.FromName("#E1E8F5");
            gv.DataBind();

            gv.RenderControl(htmlWrite);
            Response.Write(stringWrite);
            Response.Flush();
            Response.End();
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region Registration Grid Events

    #region btnSaveRegistration_Click
    /// <summary>
    /// Event to save the registration details to DB.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveRegistration_Click(object sender, EventArgs e)
    {
        try
        {
            if (ddlParticipant.SelectedIndex != -1 && ddlParticipant.SelectedIndex != 0)
            {
                bool isValid = true;

                if (txtArrDate.Text != null && txtDeptDate.Text != null)
                {
                    DateTime dtArrDatenTime = (Convert.ToDateTime(txtArrDate.Text)).AddHours(Convert.ToInt32(ddlArrTimeHrs.SelectedValue)).AddMinutes(Convert.ToInt32(ddlArrTimeMns.SelectedValue));
                    DateTime dtDepDatenTime = (Convert.ToDateTime(txtDeptDate.Text)).AddHours(Convert.ToInt32(ddlDeptTimeHrs.SelectedValue)).AddMinutes(Convert.ToInt32(ddlDeptTimeMns.SelectedValue));

                    if (dtArrDatenTime >= dtDepDatenTime)
                    {
                        csvDeptDate.ErrorMessage = ParticipantRegistrationClass.ArrDateLessThanDepDate;
                        csvDeptDate.IsValid = false;
                        isValid = false;
                    }
                }

                if (isValid)
                {
                    ParticipantRegistrationClass registerDetails = new ParticipantRegistrationClass();
                    registerDetails.EventAttendeeId = Convert.ToInt32(ddlParticipant.SelectedValue);
                    registerDetails.Q1 = ddlSalutation.SelectedValue;
                    registerDetails.Q2 = txtPrefAdd.Text.Trim();
                    registerDetails.Q3 = txtOffice.Text.Trim();
                    registerDetails.Q4 = txtCountryRep.Text.Trim();
                    registerDetails.Q5 = rblConfAtt.SelectedValue;

                    if (txtArrDate.Text != null)
                        registerDetails.Q6 = txtArrDate.Text.ToString();
                    else
                        registerDetails.Q6 = "";

                    registerDetails.Q7 = ddlArrTimeHrs.SelectedValue + ":" + ddlArrTimeMns.SelectedValue;
                    registerDetails.Q8 = txtArrAirport.Text.ToString();
                    registerDetails.Q9 = txtArrAirline.Text.ToString();

                    if (txtDeptDate.Text != null)
                        registerDetails.Q10 = txtDeptDate.Text.ToString();
                    else
                        registerDetails.Q10 = "";

                    registerDetails.Q11 = ddlDeptTimeHrs.SelectedValue + ":" + ddlDeptTimeMns.SelectedValue;
                    registerDetails.Q12 = txtDeptAirport.Text.ToString();
                    registerDetails.Q13 = txtDeptAirline.Text.ToString();
                    registerDetails.Q14 = txtHotelName.Text.ToString();
                    registerDetails.Q15 = txtHotelLocation.Text.ToString();
                    registerDetails.Q16 = txtDualHotel.Text.ToString();
                    string noOfNights = string.Empty;

                    foreach (ListItem ltNoOfNights in cblNoOfNights.Items)
                    {
                        if (ltNoOfNights.Selected)
                            noOfNights = noOfNights + ltNoOfNights.Text + ",";
                    }

                    registerDetails.Q17 = noOfNights.TrimEnd(',');
                    string otherPref = string.Empty;

                    foreach (ListItem ltOtherPref in cblOtherPref.Items)
                    {
                        if (ltOtherPref.Selected)
                            otherPref = otherPref + ltOtherPref.Text + ",";
                    }

                    registerDetails.Q18 = otherPref.TrimEnd(',');
                    registerDetails.Q19 = rblNetEvent.SelectedValue;
                    string dietReq = string.Empty;

                    foreach (ListItem ltDietReq in cblDietReq.Items)
                    {
                        if (ltDietReq.Selected)
                            dietReq = dietReq + ltDietReq.Text + ",";
                    }

                    registerDetails.Q20 = dietReq.TrimEnd(',');
                    registerDetails.Q21 = rblAvailToPlay.SelectedValue;
                    registerDetails.Q22 = txtDirectDial.Text.ToString();
                    registerDetails.Q23 = txtMobNo.Text.ToString();
                    registerDetails.Q24 = txtAssistantName.Text.ToString();
                    registerDetails.Q25 = txtAssistantTelno.Text.ToString();
                    registerDetails.Q26 = txtAssistantEmailID.Text.ToString();
                    registerDetails.Q27 = rblVisaReq.SelectedValue;
                    registerDetails.Q28 = txtSpecialReq.Text.ToString();
                    registerDetails.Q29 = txtCPEAcc.Text.ToString();
                    registerDetails.Q30 = rblCBSubmitted.SelectedValue;
                    registerDetails.Q31 = txtOtherComments.Text.ToString();
                    registerDetails.RegisteredBy = CurrentUser.GUI;
                    registerDetails.Save();

                    lblMessage.Text = ParticipantRegistrationClass.RegistrationCompleated;
                    lblMessage.ForeColor = Color.ForestGreen;

                    // Method to clear all the entered fields.
                    ddlParticipant.SelectedIndex = 0;
                    lblFirstName.Visible = false;
                    lblLastName.Visible = false;
                    lblEmailID.Visible = false;
                    lblFName.Text = string.Empty;
                    lblLName.Text = string.Empty;
                    lblEid.Text = string.Empty;

                    ClearAll();

                    // Method to bind registered participants.
                    BindRegisteredParticipants();
                }
            }
            else
            {
                lblMessage.Text = ParticipantRegistrationClass.RegistrationCompleated;
                lblMessage.ForeColor = Color.ForestGreen;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region btnClearAll_Click
    /// <summary>
    /// Event to clear all the entered fields.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnClearAll_Click(object sender, EventArgs e)
    {
        try
        {
            // Method to clear all the entered fields.
            ClearAll();
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region ClearAll
    /// <summary>
    /// Method to clear all the entered fields.
    /// </summary>
    private void ClearAll()
    {       
        ddlSalutation.SelectedIndex = -1;
        txtPrefAdd.Text = string.Empty;
        txtOffice.Text = string.Empty;
        txtCountryRep.Text = string.Empty;
        rblConfAtt.SelectedIndex = -1;
        txtArrDate.Text = string.Empty;
        ddlArrTimeHrs.SelectedIndex = 0;
        ddlArrTimeMns.SelectedIndex = 0;
        txtArrAirport.Text = string.Empty;
        txtArrAirline.Text = string.Empty;
        txtDeptDate.Text = string.Empty;
        ddlDeptTimeHrs.SelectedIndex = 0;
        ddlDeptTimeMns.SelectedIndex = 0;
        txtDeptAirport.Text = string.Empty;
        txtDeptAirline.Text = string.Empty;
        txtHotelName.Text = string.Empty;
        txtHotelLocation.Text = string.Empty;
        txtDualHotel.Text = string.Empty;
        cblNoOfNights.SelectedIndex = -1;
        cblOtherPref.SelectedIndex = -1;
        rblNetEvent.SelectedIndex = -1;
        cblDietReq.SelectedIndex = -1;
        rblAvailToPlay.SelectedIndex = -1;
        txtDirectDial.Text = string.Empty;
        txtMobNo.Text = string.Empty;
        txtAssistantName.Text = string.Empty;
        txtAssistantTelno.Text = string.Empty;
        txtAssistantEmailID.Text = string.Empty;
        rblVisaReq.SelectedIndex = -1;
        txtSpecialReq.Text = string.Empty;
        txtCPEAcc.Text = string.Empty;
        rblCBSubmitted.SelectedIndex = -1;
        txtOtherComments.Text = string.Empty;
    }
    #endregion

    #endregion

    #endregion

}